from utils import re_html_tag,get_img_url_wh
import time, requests
from lxml import html
from weibo_cos import get_all_text

'''
    mysql格式整理 针对api
'''


def weibo_format_to_mysql_api(dict_msg):
    to_mysql = {}
    to_mysql['contents'] = {}
    to_mysql['ext'] = {}
    # 是转发
    if dict_msg.get('retweeted_status'):
        if dict_msg['retweeted_status'].get('pics') and dict_msg['retweeted_status']['pics']:
            media_type = 1
        elif dict_msg['retweeted_status'].get('page_info') and dict_msg['retweeted_status']['page_info']:
            media_type = 2
        else:
            media_type = 4
        title = re_html_tag(dict_msg['text']) + '\n' + re_html_tag(
            dict_msg['retweeted_status']['text'])
        to_mysql['contents']['publish_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        to_mysql['contents']['thirdparty_id'] = f'https://m.weibo.cn/status/{dict_msg["bid"] }'
        to_mysql['contents']['contents_refter_type'] = 2
        to_mysql['contents']['title'] = title
        to_mysql['contents']['media_type'] = media_type
        to_mysql['contents']['pic_link'] = f'https://m.weibo.cn/status/{ dict_msg["bid"] }'
        to_mysql['ext'] = []

        if media_type == 1:
            try:
                for pic in dict_msg['retweeted_status']['pics']:
                    if pic['geo']:
                        to_mysql['ext'].append(
                            {'big_url': pic['large']['url'], 'url': pic['url'], 'width': pic['geo']['width'],
                            'height': pic['geo']['height'], 'video_link':'','video_ticket': ''})
                    else:
                        list_wh=get_img_url_wh(pic['url'])
                        to_mysql['ext'].append(
                            {'big_url': pic['large']['url'], 'url': pic['url'], 'width': list_wh[0],
                             'height': list_wh[1], 'video_link': '', 'video_ticket': ''})
            except KeyError:
                to_mysql['ext'] = []
        else:
            try:
                to_mysql['ext'].append({'big_url': dict_msg['retweeted_status']['page_info']['page_pic']['url'],
                                        'url': dict_msg['retweeted_status']['page_info']['page_pic']['url'],
                                        'width': 0, 'height': 0,'video_link':'',
                                        'video_ticket': f'https://m.weibo.cn/status/{dict_msg["retweeted_status"]["id"]}'})
            except KeyError:
                to_mysql['ext'] = []

    else:
        if dict_msg.get('pics') and dict_msg['pics']:
            media_type = 1
        elif dict_msg.get('page_info') and dict_msg['page_info']:
            media_type = 2
        else:
            media_type = 4
        if dict_msg['text'].find('全文</a>'):
            title = re_html_tag(dict_msg['text'])
            schemeurl = 'https://m.weibo.cn/status/' + dict_msg['mid']


        else:
            title = re_html_tag(dict_msg['text'])
        to_mysql['contents']['publish_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        to_mysql['contents']['thirdparty_id'] = f'https://m.weibo.cn/status/{dict_msg["bid"] }'
        to_mysql['contents']['contents_refter_type'] = 2
        to_mysql['contents']['title'] = title
        to_mysql['contents']['media_type'] = media_type
        to_mysql['contents']['pic_link'] = f'https://m.weibo.cn/status/{ dict_msg["bid"] }'
        to_mysql['ext'] = []

        if media_type == 1:
            try:
                for pic in dict_msg['pics']:
                    if pic['geo']:
                        to_mysql['ext'].append(
                        {'big_url': pic['large']['url'], 'url': pic['url'], 'width': pic['geo']['width'],
                         'height': pic['geo']['height'],'video_link':'', 'video_ticket': ''})
                    else:
                        list_wh=get_img_url_wh(pic['url'])
                        to_mysql['ext'].append(
                            {'big_url': pic['large']['url'], 'url': pic['url'], 'width': list_wh[0],
                             'height': list_wh[1], 'video_link': '', 'video_ticket': ''})
            except KeyError:
                to_mysql['ext'] = []
        else:
            try:
                to_mysql['ext'].append({'big_url': dict_msg['page_info']['page_pic']['url'],
                                        'url': dict_msg['page_info']['page_pic']['url'],
                                        'width': 0, 'height': 0,
                                        'video_link':'',
                                        'video_ticket': f'https://m.weibo.cn/status/{dict_msg["id"]}'})
            except KeyError:
                to_mysql['ext'] = []
    return to_mysql


def bilibili_format_to_mysql_api(dict_msg):
    to_mysql = {}
    to_mysql['contents'] = {}
    to_mysql['ext'] = {}

    to_mysql['contents']['publish_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    to_mysql['contents']['thirdparty_id'] = dict_msg["pic_url"]
    to_mysql['contents']['contents_refter_type'] = dict_msg['type']
    to_mysql['contents']['title'] = dict_msg['title']
    to_mysql['contents']['media_type'] = 2
    to_mysql['contents']['pic_link'] = dict_msg["pic_url"]
    to_mysql['ext'] = []

    to_mysql['ext'].append({'big_url': dict_msg['images'][0], 'url': dict_msg['images'][0],
                            'width': 0, 'height': 0,
                            'video_link': '',
                            'video_ticket': dict_msg['video_ticket']})
    return to_mysql

def img_format_to_mysql_api(dict_msg):
    to_mysql = {}
    to_mysql['contents'] = {}
    to_mysql['ext'] = {}
    to_mysql['contents']['publish_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    to_mysql['contents']['thirdparty_id'] = dict_msg["url"]
    to_mysql['contents']['contents_refter_type'] = dict_msg["type"]
    to_mysql['contents']['title'] = dict_msg['title']
    to_mysql['contents']['media_type'] =dict_msg['media_type']

    to_mysql['contents']['pic_link'] = dict_msg["url"]
    to_mysql['ext'] = []
    for i in dict_msg['img']:

        to_mysql['ext'].append({'big_url':i, 'url':i,
                            'width': 0, 'height': 0,
                            'video_link': dict_msg['video_link'],
                            'video_ticket': dict_msg['video_ticket']})
    return to_mysql
